package com.ityedao.Stream流;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        Collections.addAll(names, "张三丰", "张无忌", "周芷若", "张三", "赵敏", "张蔷");
        System.out.println(names);

        // 找出姓张，且是3个字的名字，存入到一个新集合中去
        List<String> list = new ArrayList<>();
        for (String name : names) {
            if(name.startsWith("张") && name.length() == 3){
                list.add(name);
            }
        }
        System.out.println(list);

        // 开始使用Stream流来解决这个问题
        List<String> list2 = names.stream().filter(s -> s.startsWith("张")).filter(o -> o.length() == 3).collect(Collectors.toList());
        System.out.println(list2);

        List<String> list3 = names.stream().filter(s -> s.startsWith("张") && s.length() == 3).collect(Collectors.toList());
        System.out.println(list3);
    }
}
